#ifndef SROS_DEVICE_DRIVER_DEVICE_HPP
#define SROS_DEVICE_DRIVER_DEVICE_HPP

#include "config/driver_config.hpp"

#include "device_impl.hpp"
#include "devices/detail/driver_impl.hpp"

#include <vector>
#include <mutex>

namespace sros::device
{
    class McuDevice;

    class DriverDevice final : public device_impl<cfg::DriversCfg>
    {
    public:
        using EventCb = std::function<void(const uint8_t &ev_id, const uint8_t &dir,const uint32_t &data, const std::string &msg)>;

        DriverDevice(std::shared_ptr<McuDevice> mcu_device);
        ~DriverDevice() override;

        RetBool init() override;

        void run() override;

        void close() override;

        void parse(const device::proto::RspCanMsg &);

        RetBool data_up(const cfg::DriverDir &dir, detail::driver_impl::DataUp &data);
        RetBool data_down(const cfg::DriverDir &dir, detail::driver_impl::DataDown &data);
        RetBool info(const cfg::DriverDir &dir, detail::Info &info);
        RetBool param(const cfg::DriverDir &dir, detail::driver_impl::Param &param);
        RetBool set_spd(const uint8_t &dir, const int32_t &spd);
        RetBool set_current(const cfg::DriverDir &dir, const int32_t &current);
        RetBool set_control(const uint16_t &control_word);

        void install_event(EventCb cb);

    protected:
        // 配置加载
        RetBool load_config() override;
        
    private:
        // 根据配置初始化所有驱动实例
        RetBool init_drivers_();

    private:
        std::mutex mtx_;
        std::shared_ptr<McuDevice> mcu_;
        
        std::shared_ptr<detail::driver_impl> h_driver_;
        std::shared_ptr<detail::driver_impl> v_driver_;
        std::shared_ptr<detail::driver_impl> p_driver_;
        std::shared_ptr<detail::driver_impl> r_driver_;
    };
}

#endif